home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / syscall / RCS / procEnviron.c,v < prev    next >
Encoding:
Text File  |  1988-07-30  |  10.1 KB  |  461 lines

  1. head     1.5;
  2. access   ;
  3. symbols  ;
  4. locks    ; strict;
  5. comment  @ * @;
  6.  
  7.  
  8. 1.5
  9. date     88.07.29.17.08.48;  author ouster;  state Exp;
  10. branches ;
  11. next     1.4;
  12.  
  13. 1.4
  14. date     88.07.25.11.15.25;  author ouster;  state Exp;
  15. branches ;
  16. next     1.3;
  17.  
  18. 1.3
  19. date     88.06.21.11.19.51;  author ouster;  state Exp;
  20. branches ;
  21. next     1.2;
  22.  
  23. 1.2
  24. date     88.06.21.11.15.02;  author ouster;  state Exp;
  25. branches ;
  26. next     1.1;
  27.  
  28. 1.1
  29. date     88.06.19.14.29.29;  author ouster;  state Exp;
  30. branches ;
  31. next     ;
  32.  
  33.  
  34. desc
  35. @@
  36.  
  37.  
  38. 1.5
  39. log
  40. @Lint.
  41. @
  42. text
  43. @/* 
  44. * procEnviron.c --
  45. *
  46. *    Routines to map from the original environment-related kernel
  47. *    calls into the standard unix operations on the stack.  The
  48. *    old kernel calls are still available by changing the name of the
  49. *    call to Proc_OLD...
  50. *
  51. *    If a process's environment on the stack is nonexistent, then it
  52. *    assumes it has been called by Proc_Exec rather than Proc_ExecEnv,
  53. *    and getenv, et al., call Proc_OLD*.  This will go away once all
  54. *    programs pass the environment on the stack.
  55. *
  56. * Copyright 1987 Regents of the University of California
  57. * All rights reserved.
  58. */
  59.  
  60. #ifndef lint
  61. static char rcsid[] = "$Header: procEnviron.c,v 1.4 88/07/25 11:15:25 ouster Exp $ SPRITE (Berkeley)";
  62. #endif not lint
  63.  
  64.  
  65. #include <sprite.h>
  66. #include <bstring.h>
  67. #include <proc.h>
  68. #include <status.h>
  69. #include <stdio.h>
  70. #include <stdlib.h>
  71. #include <string.h>
  72.  
  73. /*
  74.  * Library imports:
  75.  */
  76.  
  77. extern char **environ;
  78. extern void unsetenv();
  79.  
  80.  
  81. /*
  82. *----------------------------------------------------------------------
  83. *
  84. * Proc_GetEnvironVar --
  85. *
  86. *    Routine to map from the original Sprite call into getenv.
  87. *
  88. * Results:
  89. *    None.
  90. *
  91. * Side effects:
  92. *    The process's environment is modified.
  93. *
  94. *----------------------------------------------------------------------
  95. */
  96.  
  97. ReturnStatus
  98. Proc_GetEnvironVar(environVar)
  99. Proc_EnvironVar    environVar;    /* Variable to add to environment. */
  100. {
  101.     char *value;
  102.     extern char *getenv();
  103.  
  104.     value = getenv(environVar.name);
  105.     if (value == NULL) {
  106.     return(FAILURE);
  107.     }
  108.     strncpy(environVar.value, value, PROC_MAX_ENVIRON_VALUE_LENGTH);
  109.     return(SUCCESS);
  110. }
  111.  
  112. /*
  113. * ----------------------------------------------------------------------------
  114. *
  115. * Proc_GetEnvironRange --
  116. *
  117. *    Return as many environment variables as possible in the given range.  
  118. *    Variables are numbered from 0.  The actual number of environment 
  119. *    variables returned is returned in numActualVarsPtr.  The null 
  120. *    string is returned for any environment variables that are not set.
  121. *
  122. * Results:
  123. *    Error status if some error occcurs.  SUCCESS otherwise.
  124. *
  125. * Side effects:
  126. *    None.
  127. *
  128. * ----------------------------------------------------------------------------
  129. */
  130.  
  131. ReturnStatus
  132. Proc_GetEnvironRange(first, last, envArray, numActualVarsPtr)
  133.     int                first;            /* First var to 
  134.                              * retrieve. */
  135.     int             last;            /* Last var to 
  136.                              * retrieve. */
  137.     register    Proc_EnvironVar    *envArray;        /* Where to 
  138.                              * store vars.*/
  139.     int                *numActualVarsPtr;    /* Number of vars
  140.                              * retrieved. */
  141. {
  142.     ReturnStatus            status = SUCCESS;
  143.     int                    i;
  144.     char                 **envPtr;
  145.     char                *varPtr;
  146.     char                *namePtr;
  147.  
  148.     /*
  149.      * We might not be set up with a valid environment on the stack. In
  150.      * this case, punt and make the kernel call.
  151.      */
  152.     if (environ == NULL || *environ == NULL) {
  153.     status = Proc_OLDGetEnvironRange(first, last, envArray,
  154.                      numActualVarsPtr);
  155.     return(status);
  156.     }
  157.  
  158.     if (last < first || first < 0) {
  159.     return(SYS_INVALID_ARG);
  160.     }
  161.  
  162.     /*
  163.      * Copy out the environment variables.
  164.      */
  165.     envPtr = environ;
  166.     for (i = 0; i < first; i++) {
  167.     if (*envPtr == NULL) {
  168.         *numActualVarsPtr = 0;
  169.         return(SUCCESS);
  170.     }
  171.     envPtr++;
  172.     }
  173.     for (i = first; i <= last && *envPtr != NULL; i++, envPtr++, envArray++) {
  174.     varPtr = *envPtr;
  175.     if (*varPtr == NULL) {
  176.         envArray->name[0] = NULL;
  177.         envArray->value[0] = NULL;
  178.     } else {
  179.         char *value;
  180.         
  181.         value = index(varPtr, '=');
  182.         if (value != NULL) {
  183.         (void) strcpy(envArray->value, (char *) (value + 1));
  184.         } else {
  185.         envArray->value[0] = NULL;
  186.         }
  187.  
  188.         namePtr = envArray->name;
  189.         while ((*varPtr != '\0') && (*varPtr != '=')) {
  190.         *namePtr = *varPtr;
  191.         varPtr++, namePtr++;
  192.         }
  193.         *namePtr = '\0';
  194.     }
  195.     }
  196.     *numActualVarsPtr = i - first;
  197.     return(status);
  198. }
  199.  
  200.  
  201.  
  202. /*
  203.  *----------------------------------------------------------------------
  204.  *
  205.  * Proc_SetEnviron --
  206.  *
  207.  *    Routine to map from the original Sprite call into setenv.
  208.  *
  209.  * Results:
  210.  *    None.
  211.  *
  212.  * Side effects:
  213.  *    The process's environment is modified.
  214.  *
  215.  *----------------------------------------------------------------------
  216.  */
  217.  
  218. ReturnStatus
  219. Proc_SetEnviron(environVar)
  220.     Proc_EnvironVar    environVar;    /* Variable to add to environment. */
  221. {
  222.     setenv(environVar.name, environVar.value);
  223.     return(SUCCESS);
  224. }
  225.  
  226.  
  227.  /*
  228.  *----------------------------------------------------------------------
  229.  *
  230.  * Proc_UnsetEnviron --
  231.  *
  232.  *    Routine to map from the original Sprite call into unsetenv.
  233.  *
  234.  * Results:
  235.  *    None.
  236.  *
  237.  * Side effects:
  238.  *    The process's environment is modified.
  239.  *
  240.  *----------------------------------------------------------------------
  241.  */
  242.  
  243. ReturnStatus
  244. Proc_UnsetEnviron(environVar)
  245.     Proc_EnvironVar    environVar;    /* Variable to add to environment. */
  246. {
  247.     unsetenv(environVar.name);
  248.     return(SUCCESS);
  249. }
  250.  
  251.  
  252. /*
  253.  *----------------------------------------------------------------------
  254.  *
  255.  * Proc_InstallEnviron --
  256.  *
  257.  *    Routine to create a new environment, from scratch.  Formerly,
  258.  *    this would be a system call, but now we just set up the global
  259.  *    variable 'environ'.  Note, this doesn't free the old environ or
  260.  *    the data it pointed to, since the data might have been allocated
  261.  *    on the stack rather than with malloc.  However, this routine is not
  262.  *     likely to be called more than once, if that.
  263.  *
  264.  *    It's possible that the caller will be installing its current
  265.  *    environment, in which case no changes would be necessary (since
  266.  *    each process now gets its own copy anyway, making a private copy
  267.  *    via this routine is unnecessary).  However, let's just create
  268.  *    the new environment anyway: this is a temporary placeholder after
  269.  *    all.
  270.  *
  271.  * Results:
  272.  *    None.
  273.  *
  274.  * Side effects:
  275.  *    The process's environment is modified.
  276.  *
  277.  *----------------------------------------------------------------------
  278.  */
  279.  
  280. ReturnStatus
  281. Proc_InstallEnviron(newEnviron, numVars)
  282.     Proc_EnvironVar    newEnviron[];    /* Array of new environment variables */
  283.     int         numVars;     /* Size of array */
  284. {
  285.     int i;
  286.     char *newVar;
  287.     extern char *malloc();
  288.     Proc_EnvironVar *envPtr;    /* pointer into array of vars */
  289.  
  290.     environ = (char **) malloc((unsigned) ((numVars + 1) * sizeof(char *)));
  291.     for (i = 0, envPtr = newEnviron; i < numVars; i++, envPtr++) {
  292.     newVar = malloc ((unsigned) (strlen (envPtr->name) +
  293.                      strlen (envPtr->value) + 2));
  294.     if (newVar == NULL) {
  295.         return (FAILURE);
  296.     }
  297.     (void) sprintf (newVar, "%s=%s", envPtr->name, envPtr->value);
  298.     environ[i] = newVar;
  299.     }
  300.     environ[numVars] = NULL;
  301.     return(SUCCESS);
  302. }
  303.  
  304.  
  305. /*
  306.  *----------------------------------------------------------------------
  307.  *
  308.  * Proc_CopyEnviron --
  309.  *
  310.  *    Routine to copy the environment. If environ is set, then do nothing.
  311.  *    otherwise, we're using the system-wide environment, so call
  312.  *    the original kernel routine.
  313.  *
  314.  * Results:
  315.  *    Propagated from the kernel call, or SUCCESS.
  316.  *
  317.  * Side effects:
  318.  *    None.
  319.  *
  320.  *----------------------------------------------------------------------
  321.  */
  322.  
  323. ReturnStatus
  324. Proc_CopyEnviron()
  325. {
  326. #define COMPAT_ENV
  327. #ifndef COMPAT_ENV
  328.     if (environ == NULL || *environ == NULL) {
  329. #endif COMPAT_ENV
  330.     return(Proc_OLDCopyEnviron());
  331. #ifndef COMPAT_ENV
  332.     }
  333.     return(SUCCESS);
  334. #endif COMPAT_ENV
  335. }
  336.  
  337. /*
  338.  *----------------------------------------------------------------------
  339.  *
  340.  * Proc_FetchGlobalEnv --
  341.  *
  342.  *    Reads the process's environment from the kernel into new storage
  343.  *    allocated on the heap (to be used to pass environment into 
  344.  *    Proc_ExecEnv).
  345.  *
  346.  *    The strings are done in the traditional fashion of 'name=value', though
  347.  *    they appear on the stack in reverse order.
  348.  *
  349.  * Results:
  350.  *    The address of the start of the vector. 
  351.  *
  352.  * Side Effects:
  353.  *    Memory is allocated to hold the environment.
  354.  *
  355.  *----------------------------------------------------------------------
  356.  */
  357. char **
  358. Proc_FetchGlobalEnv()
  359. {
  360.     register char    **vecPtr;
  361.     register int    numVariables;
  362.     register int    varNum;
  363.  
  364.     char        value[PROC_MAX_ENVIRON_VALUE_LENGTH];
  365.     char        name[PROC_MAX_ENVIRON_NAME_LENGTH];
  366.     Proc_EnvironVar    var;
  367.     char        *tempEnv[PROC_MAX_ENVIRON_SIZE];
  368.     int        i;
  369.  
  370.     vecPtr = tempEnv;
  371.     varNum = 0;
  372.     var.name = name; var.value = value;
  373.  
  374.     while ((Proc_OLDGetEnvironRange(varNum, varNum, &var, &i) == SUCCESS) && i) {
  375.     varNum += 1;
  376.     if (name[0]){
  377.         *vecPtr = malloc((unsigned) (strlen(name)+1+
  378.                 strlen(value)+1));
  379.         (void) strcpy(*vecPtr, name);
  380.         (void) strcat(*vecPtr, "=");
  381.         (void) strcat(*vecPtr, value);
  382.         vecPtr++;
  383.     }
  384.     }
  385.     *vecPtr = (char *)NULL;
  386.     numVariables = vecPtr - tempEnv + 1;
  387.     vecPtr = (char **) malloc((unsigned) (numVariables*sizeof(char *)));
  388.     bcopy((char *) tempEnv, (char *) vecPtr, numVariables*sizeof(char *));
  389.  
  390.     return (vecPtr);
  391. }
  392. @
  393.  
  394.  
  395. 1.4
  396. log
  397. @Lint.
  398. @
  399. text
  400. @d19 1
  401. a19 1
  402. static char rcsid[] = "$Header: procEnviron.c,v 1.3 88/06/21 11:19:51 ouster Exp $ SPRITE (Berkeley)";
  403. d248 1
  404. a248 1
  405.     environ = (char **) malloc((numVars + 1) * sizeof(char *));
  406. d335 2
  407. a336 2
  408.         *vecPtr = malloc(strlen(name)+1+
  409.                 strlen(value)+1);
  410. d345 2
  411. a346 2
  412.     vecPtr = (char **) malloc(numVariables*sizeof(char *));
  413.     bcopy(tempEnv, vecPtr, numVariables*sizeof(char *));
  414. @
  415.  
  416.  
  417. 1.3
  418. log
  419. @Must include status.h.
  420. @
  421. text
  422. @d19 1
  423. a19 1
  424. static char rcsid[] = "$Header: procEnviron.c,v 1.2 88/06/21 11:15:02 ouster Exp $ SPRITE (Berkeley)";
  425. d24 1
  426. d27 2
  427. a34 3
  428. extern char *malloc();
  429. extern void bcopy();
  430.  
  431. d36 1
  432. @
  433.  
  434.  
  435. 1.2
  436. log
  437. @Cleanup header usage.
  438. @
  439. text
  440. @d19 1
  441. a19 1
  442. static char rcsid[] = "$Header: procEnviron.c,v 1.1 88/06/19 14:29:29 ouster Exp $ SPRITE (Berkeley)";
  443. d25 1
  444. @
  445.  
  446.  
  447. 1.1
  448. log
  449. @Initial revision
  450. @
  451. text
  452. @d19 1
  453. a19 1
  454. static char rcsid[] = "$Header: procEnviron.c,v 1.1 88/03/30 16:33:36 douglis Exp $ SPRITE (Berkeley)";
  455. d23 3
  456. a25 3
  457. #include "sprite.h"
  458. #include "proc.h"
  459. #include <strings.h>
  460. @
  461.